<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('主子表提交')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form id="form-add" class="form-horizontal">
            <h4 class="form-header h4">订购单信息</h4>
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">订单编号：</label>
                        <div class="col-sm-8">
                            <input name="orderIndent" id="orderIndent" class="form-control" value="" type="text" maxlength="30" readonly>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">客户姓名：</label>
                        <div class="col-sm-8">
                            <input name="customerName" placeholder="请输入客户名称" class="form-control" type="text" maxlength="30">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">订购时间：</label>
                        <div class="col-sm-8">
                            <div class="input-group date">
                                <input name="orderTime" class="form-control" placeholder="yyyy-MM-dd hh:mm:ss" readonly type="text" th:value="${#dates.format(new java.util.Date().getTime(), 'yyyy-MM-dd hh:mm:ss')}">
                                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">创建人：</label>
                        <div class="col-sm-8">
                            <input name="sysOperUserId" class="form-control" type="hidden"  th:value="${user.userId}">
                            <input name="" class="form-control" type="text" readonly  th:value="${user.userName}">
                        </div>
                    </div>


            <div class="form-group">
                <label class="col-sm-4 control-label is-required">审核状态：</label>
                <div class="col-sm-8">
                    <input name="chkStatus" value="0"  class="form-control" type="hidden" maxlength="30">
                    <input  value="未审核" readonly class="form-control" type="text" maxlength="30">
                </div>
            </div>


            <div class="form-group">
                        <label class="col-sm-4 control-label is-required">总金额：</label>
                        <div class="col-sm-8">
                            <input id="orderMoney" name="orderMoney"  class="form-control" type="text" maxlength="11" readonly>
                        </div>
            </div>

            <h4 class="form-header h4">商品数据</h4>
            <div class="row">
                <div class="col-sm-12">
                    <button type="button" class="btn btn-white btn-sm" onclick="addColumn()"><i class="fa fa-plus"> 增加</i></button>
                    <button type="button" class="btn btn-white btn-sm" onclick="sub.delColumn()"><i class="fa fa-minus"> 删除</i></button>
                    <div class="col-sm-12 select-table table-striped">
					    <table id="bootstrap-table" onkeyup="foo()"></table>
<!--                        onkeyup=foo()-->
					</div>
                </div>
            </div>
        </form>
    </div>
      
    <div class="row">
        <div class="col-sm-offset-5 col-sm-10">
        <button type="button" class="btn btn-white btn-sm"  id="totalMoeny" onclick="addMoney()"><i class="fa fa-plus"> 计算所有商品总金额</i></button>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:src="@{/js/jquery.tmpl.js}"></script>
	<script th:inline="javascript">
	    $(function() {
	    	// 初始化数据, 可以由后台传过来
	    	var data = [
	    	{
                brandName: "3",
                typeName: "2",
                productModel: "苹果13",
                num: "1",
                unit: "台",
                price: "1",
                total: "1",
	    	}];
		    var options = {
		    	data: data,
                pagination: false,
		        showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                sidePagination: "client",
		        columns: [{
		            checkbox: true
		        },
		        {
		        	field: 'index',
		        	align: 'center',
		        	title: "序号",
                    formatter: function (value, row, index) {
                    	var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                    	var columnId = $.common.sprintf("<input type='hidden' name='' id='mkOrderIndent' value='%s'>", index, row.id);
                    	return columnIndex + $.table.serialNumber(index) + columnId;
                    }
                },
                    {
                        field: 'brandName',
                        align: 'center',
                        title: '品牌',
                        formatter: function(value, row, index) {
                            var data = [{ index: index, type: value }];
                            return $("#goodsTypeTpl1").tmpl(data).html();
                        }
                    },
                    {
                        field: 'typeName',
                        align: 'center',
                        title: '类型',
                        formatter: function(value, row, index) {
                            var data = [{ index: index, type: value }];
                            return $("#goodsTypeTpl2").tmpl(data).html();
                        }
                    },
                    {
                        field: 'productModel',
                        align: 'center',
                        title: '型号',
                        formatter: function(value, row, index) {
                            var data = [{ index: index, type: value }];
                            return $("#goodsTypeTpl3").tmpl(data).html();
                        }
                    },
                    {
                        field: 'num',
                        align: 'center',
                        title: '数量',
                        formatter: function(value, row, index) {
                            var html = $.common.sprintf("<input class='form-control' type='text' id='num' name='poProduct2List[%s].num' value='%s'>", index, value);
                            return html;
                        }
                    },
                    {
                        field: 'unit',
                        align: 'center',
                        title: '单位',
                        formatter: function(value, row, index) {
                            var html = $.common.sprintf("<input class='form-control' type='text' name='poProduct2List[%s].unit' value='台' >", index, value);
                            return html;
                        }
                    },
                    {
                        field: 'price',
                        align: 'center',
                        title: '商品价格',
                        formatter: function(value, row, index) {
                            var html = $.common.sprintf("<input class='form-control price' type='text' id='price'  name='poProduct2List[%s].price' value='%s'>", index, value);
                            return html;
                        }
                    },
                    {
                        field: 'total',
                        align: 'center',
                        title: '金额',
                        formatter: function (value, row, index) {
                            var html = $.common.sprintf("<input class='form-control money' type='text' readonly id='money' name='poProduct2List[%s].total' value='%s'>", index, value);
                            return html;
                        }
                    }]
            };
		    $.table.init(options);
		});
	    
        var prefix = ctx + "market/order"
        $("#form-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-add').serialize());
            }
        }


        
        function addColumn() {
        	var row = {
                brandName: "",
                typeName: "",
                productModel: "",
                num: "",
                unit: "",
                price: "",
                total: "",
            }
        	sub.addColumn(row);
        }

        $("input[name='birthday']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("#bootstrap-table").on("post-body.bs.table", function (e, args) {
        	$("input[name$='date']").datetimepicker({
	            format: "yyyy-mm-dd",
	            minView: "month",
	            autoclose: true,
	            pickerPosition:'top-right'
	        });
    	});

        function getPurId(){//构建方法
            var date = new Date();
            return "DJ"+date.getTime();
        }
        document.getElementById("orderIndent").value = getPurId();//赋给input文本输入框
        function getPurTime(){//构建方法
            var today=new Date();//new 出当前时间
            var h=today.getFullYear();//获取年
            var m=today.getMonth()+1;//获取月
            var d=today.getDate();//获取日
            var H = today.getHours();//获取时
            var M = today.getMinutes();//获取分
            var S = today.getSeconds();//获取秒
            return h+"-"+m+"-"+d+" "+H+":"+M+":"+S; //返回 年-月-日 时:分:秒
        }
        document.getElementById("orderTime").value = getPurTime();//将获取到的 年-月-日 时:分:秒 赋给input文本输入框
        var orderIndent=$("orderIndent").value;
        $("mkOrderIndent").val(orderIndent);

        //计算单行金额
        function foo(e) {
            e = e || window.event;
            var el = e.Target || e.srcElement
            while(el.nodeName != 'TR') el = el.parentNode;
            var o = el.getElementsByTagName('input');
            o[6].value = o[3].value * o[5].value;//计算单行金额
        }

        function addMoney() {
            var array = {};//定义一个数组
            var array = document.getElementsByClassName('money');
            var total = 0;
            for(j = 0,len=array.length; j < len; j++) {
                total = parseInt(array[j].value) + total;
            }
            document.getElementsByName("orderMoney")[0].value=total;
        }

    </script>

</body>
</html>
<!-- 品牌 -->
<script id="goodsTypeTpl1" type="text/x-jquery-tmpl">
<div>
<select class='form-control' name='poProduct2List[${index}].poBrandId'>
                <option value="">请选择</option>
                <option value="3">海尔1</option>
                <option value="4">联想</option>
                <option value="11">联想1</option>
</select>
</div>
</script>

<!-- 类型 -->
<script id="goodsTypeTpl2" type="text/x-jquery-tmpl">
<div>
<select class='form-control' name='poProduct2List[${index}].poTypeId'>
                <option  value="">请选择</option>
                <option  value="1">笔记本电脑</option>
                <option  value="2">台式电脑</option>
                <option  value="3">手机</option>
</select>
</div>
</script>
<!-- 型号 -->
<script id="goodsTypeTpl3" type="text/x-jquery-tmpl">
<div>
<select class='form-control' name='poProduct2List[${index}].productModel'>
                <option value="">请选择</option>
                <option value="苹果13">苹果13</option>
                <option value="拯救者y7000">拯救者y7000</option>
                <option value="小米144寸臻享版平板">小米144寸臻享版平板</option>
</select>
</div>
</script>